//
//  HttpHelper.m
//  qiuShiJiangHu
//
//  Created by xalo on 16/1/23.
//  Copyright © 2016年 糗事江湖项目组. All rights reserved.
//

#import "HttpHelper.h"
#import <AFHTTPSessionManager.h>
#import <AFNetworking/AFNetworking.h>
#import "HomePageModel.h"

@interface HttpHelper ()

@property (nonatomic,strong) NSMutableArray *modelArray;

@end



@implementation HttpHelper

static HttpHelper *hendel = nil;

//网络请求单例
+(HttpHelper *)sharedManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        hendel = [[HttpHelper alloc]init];
        
    });

    return hendel;
}


//数据解析
-(void)fectchSourceWithUrl:(NSString *)url block:(BLOCK)block
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{        
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",nil];
        
        [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
           NSDictionary *dic = [NSDictionary dictionary];
            
            dic = responseObject;
            
            NSMutableArray *array = [NSMutableArray array];
            
            for (NSString *key in dic) {
                if ([key isEqualToString:@"items"]) {
                    
                    array = dic[@"items"];
                }
            }
            
            for (NSDictionary *dic1 in array) {
                
                HomePageModel *model = [[HomePageModel alloc]init];
                
                [model setValuesForKeysWithDictionary:dic1];
                
                [self.modelArray addObject:model];
                
//                NSLog(@"%@",_modelArray);
                
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                block();
            });
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            NSLog(@"请求失败:%@",error);
            
        }];
        
    });
    
}

-(NSMutableArray *)fectchSourceWithUrl:(NSString *)url
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",nil];
        
        [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSDictionary *dic = [NSDictionary dictionary];
            
            dic = responseObject;
            
            NSMutableArray *array = [NSMutableArray array];
            
            for (NSString *key in dic) {
                if ([key isEqualToString:@"items"]) {
                    
                    array = dic[@"items"];
                }
            }
            
            for (NSDictionary *dic1 in array) {
                
                HomePageModel *model = [[HomePageModel alloc]init];
                
                [model setValuesForKeysWithDictionary:dic1];
                
                [self.modelArray addObject:model];
            }
           
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            NSLog(@"请求失败:%@",error);
            
        }];
        
    });
    return self.modelArray;
}












//懒加载
-(NSMutableArray *)modelArray
{
    if (!_modelArray) {
        
        _modelArray = [NSMutableArray array];
        
    }

    return _modelArray;
}

//返回数组个数
-(NSInteger)countOfModelArray
{
    return _modelArray.count;

}


//根据下标返回对应的model
-(HomePageModel *)returnModelWithIndex:(NSInteger)index
{
    return _modelArray[index];

}


//返回一个数组

-(NSMutableArray *)returnArray
{
    return _modelArray;

}



























@end
